package org.apache.doris.demo.spring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.doris.demo.spring.model.UserTag;
import org.apache.doris.demo.spring.service.TagService;
import org.apache.doris.demo.spring.service.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class UserTagController {
    @Autowired
    private UserTagService userTagService;
    @Autowired
    private TagService tagService;

    @GetMapping("/userTag")
    public String list(Model model) {
        model.addAttribute("userTags", userTagService.list());
        return "userTag/list";
    }

    @GetMapping("/userTag/{userId}")
    public String get(@PathVariable String userId, Model model) {
        QueryWrapper<UserTag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("userid", userId);
        List<UserTag> list = userTagService.list(updateWrapper);
        model.addAttribute("userTags", list);
        model.addAttribute("userid", userId);
        model.addAttribute("tagTypeMap",tagService.getTagTypeMap());
        return "userTag/manage/list";
    }
//
//    @GetMapping("/userTag/delete/{distinctId}")
//    public String deleteTag(@PathVariable String distinctId) {
//        Map<String, Object> userTag = new HashMap<>();
//        userTag.put("distinct_id", distinctId);
//        userTagService.removeByMap(userTag);
//        return "redirect:/userTag";
//    }
    @GetMapping("/userTag/userProfile/{userid}")
    public String userProfile(@PathVariable String userid, Model model) {
        List<UserTag> list = userTagService.list(new QueryWrapper<UserTag>().eq("userid", userid));
        List<String> tags = list.stream().map(UserTag::getTagValue).collect(Collectors.toList());
        model.addAttribute("tags", tags);
        model.addAttribute("userid", userid);
        return "redirect:/userProfile/detail";
    }
    @GetMapping("/userTag/edit/{distinctId}")
    public String edit(@PathVariable String distinctId, Model model) {
        QueryWrapper<UserTag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("distinct_id", distinctId);
        List<UserTag> list = userTagService.list(updateWrapper);
        if (list.size() > 0) {
        UserTag userTag = list.get(0);
        model.addAttribute("userTag", userTag);
        return "userTag/edit";
        } else {
            return "userTag/list";
        }
    }

    @GetMapping("/userTag/add")
    public String addForm(Model model) {
        model.addAttribute("userTag", new UserTag());
        model.addAttribute("tagTypeMap",tagService.getTagTypeMap());
        return "userTag/add";
    }

    @PostMapping("/userTag/add")
    public String add(@ModelAttribute UserTag userTag) {
        userTag.setInsertTime(LocalDateTime.now());
        userTagService.save(userTag);
        return "redirect:/userTag/";
    }
    @PostMapping("/userTag/add/{userId}")
    public String addTag(@PathVariable String userId,@ModelAttribute UserTag userTag) {
        userTag.setInsertTime(LocalDateTime.now());
        userTag.setUserId(Long.valueOf(userId));
        userTagService.save(userTag);
        return "redirect:/userTag/{userId}";
    }

    @PutMapping("/userTag/{distinctId}")
    public String update(@PathVariable String distinctId, @ModelAttribute UserTag userTag) {
        userTag.setDistinctId(distinctId);
        QueryWrapper<UserTag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("userid", userTag.getUserId())
                .eq("tagname",userTag.getTagName())
                .eq("discinct_id",userTag.getDistinctId())
                .eq("tagvalue",userTag.getTagValue())
                .eq("subject",userTag.getSubject())
                .eq("inserttime",userTag.getInsertTime());
        userTagService.update(updateWrapper);
        return "redirect:/userTag/{distinctId}";
    }

    @DeleteMapping("/userTag/{distinctId}")
    public String delete(@PathVariable String distinctId) {
        QueryWrapper<UserTag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("discinctId", distinctId);
//        UserTag one = userTagService.getOne(updateWrapper);
        userTagService.remove(updateWrapper);
        return "redirect:/userTag";
    }
    @PostMapping("/userTag/delete/{userId}/{distinctId}")
    public String delete(@PathVariable String userId,@PathVariable String distinctId) {
        HashMap<String, Object> h = new HashMap<>();
        h.put("userid", userId);
        h.put("distinct_id", distinctId);
        userTagService.deleteByMap(h);
        return "redirect:/userTag/"+userId;
    }
//    @PostMapping("/userTag/delete/{userId}")
//    public String deleteAll(@PathVariable String userId) {
//        HashMap<String, Object> h = new HashMap<>();
//        h.put("userid", userId);
//        userTagService.deleteByMap(h);
////        userTagService.remove(updateWrapper);
//        return "redirect:/userTag/"+userId;
//    }
    @ResponseBody
    @GetMapping("/userTag/search/{userid}")
    public List<UserTag> searchTags(@PathVariable ("userid") String userid) {
        QueryWrapper<UserTag> userTagQueryWrapper = new QueryWrapper<>();
        userTagQueryWrapper.eq("userid", userid);
        return userTagService.list(userTagQueryWrapper);
    }
    @ResponseBody
    @GetMapping("/userTag/search/")
    public List<UserTag> searchAllTags() {
//        QueryWrapper<UserTag> userTagQueryWrapper = new QueryWrapper<>();
//        userTagQueryWrapper.eq("userid", userid);
        return userTagService.list(null);
    }
}